﻿using System.Web.Mvc;
using System.Web.Routing;

namespace $rootnamespace$.Util
{
    public class RequireAuthenticationAttribute : FilterAttribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            // If the user is not authenticated the result view will be the HrdPage
            if (!filterContext.HttpContext.Request.IsAuthenticated)
            {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
                {
                    action = "LogOn",
                    controller = "Account",
                    returnUrl = filterContext.HttpContext.Request.Url.AbsoluteUri

                }));
            }

            // Request is already authenticated. We don't need to do anything
        }
    }
}